package com.fgs.common.widget;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;


import com.fgs.common.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fgs
 * @date 2020/4/11
 * @description
 *
**/

public class TextChange {
    private List<TextWatcherInfo> editTextList;
    /**
     * 监听需要操作的view
     */
    private View view;
    private CheckBox checkBox;
    /**
     * 字符个数显示
     */
    private TextView numberTextView;
    /**
     * 监听的最大字符数
     */
    private int number;

    public TextChange(Builder builder) {
        this.editTextList = builder.editTextList;
        this.view = builder.view;
        this.checkBox = builder.checkBox;
        this.numberTextView = builder.numberTextView;
        this.number = builder.number;
        init();
    }

    private void init() {
        if (editTextList.size() > 0) {
            for (TextWatcherInfo textWatcherInfo : editTextList) {
                textWatcherInfo.getEditText().addTextChangedListener(new TextChangeListener(new OnTextChangeListener() {
                    @Override
                    public void onTextChange(String string) {
                        if (checkBox == null) {
                            if (view != null) {
                                view.setEnabled(checkAllEditText());
                                view.setClickable(checkAllEditText());
                                view.setSelected(checkAllEditText());
                            }
                        } else {
                            if (view != null) {
                                view.setEnabled(checkAllEditTextAndCheckBox());
                                view.setClickable(checkAllEditTextAndCheckBox());
                                view.setSelected(checkAllEditTextAndCheckBox());
                            }
                        }
                        if (numberTextView != null && string != null && string.length() > 0) {
                            numberTextView.setText(string.length() + "/" + number);
                            if (string.length() == number) {
                                ToastUtil.showShortToast("输入字符达到了最大限制");
                            }
                        }
                    }
                }));
            }
        }
        if (checkBox != null) {
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (view == null) return;
                    view.setEnabled(isChecked && checkAllEditText());
                    view.setClickable(isChecked && checkAllEditText());
                    view.setSelected(isChecked && checkAllEditText());
                }
            });
        }
    }

    public static class Builder {

        public Builder() {
        }

        private List<TextWatcherInfo> editTextList = new ArrayList<>();
        /**
         * 监听需要操作的view
         */
        private View view;
        private CheckBox checkBox;
        /**
         * 字符个数显示
         */
        private TextView numberTextView;
        /**
         * 监听的最大字符数
         */
        private int number;

        /**
         * 添加需要监听的EditText
         *
         * @param editText
         */
        public Builder addEditText(EditText editText) {
            addEditText(editText, 1);
            return this;
        }

        /**
         * 添加需要监听的EditText
         *
         * @param editText
         * @param length   需要监听改变的字符的长度
         */
        public Builder addEditText(EditText editText, int length) {
            TextWatcherInfo textWatcherInfo = new TextWatcherInfo();
            textWatcherInfo.setEditText(editText);
            textWatcherInfo.setListenLength(length);
            editTextList.add(textWatcherInfo);
            return this;
        }

        /**
         * 需要通过监听EditText来改变状态的view
         *
         * @param view
         * @return
         */
        public Builder addListenView(View view) {
            this.view = view;
            return this;
        }

        /**
         * 添加需要监听的CheckBox
         *
         * @param checkBox
         */
        public Builder addCheckBox(CheckBox checkBox) {
            this.checkBox = checkBox;
            return this;
        }

        /**
         * 添加需要监听字符个数的TextView
         *
         * @param textView
         */
        public Builder addTextView(TextView textView, int number) {
            this.numberTextView = textView;
            this.number = number;
            return this;
        }

        public TextChange build() {
            return new TextChange(this);
        }
    }

    private boolean checkAllEditText() {
        for (TextWatcherInfo textWatcherInfo : editTextList) {
            String text = textWatcherInfo.getEditText().getText().toString().trim();
            int listenLength = textWatcherInfo.getListenLength();
            if (text != null && text.length() >= listenLength) {
                continue;
            } else {
                return false;
            }

        }
        return true;
    }

    private boolean checkAllEditTextAndCheckBox() {
        for (TextWatcherInfo textWatcherInfo : editTextList) {
            String text = textWatcherInfo.getEditText().getText().toString().trim();
            int listenLength = textWatcherInfo.getListenLength();
            if (text != null && text.length() >= listenLength && checkBox.isChecked()) {
                continue;
            } else {
                return false;
            }
        }
        return true;
    }

    private class TextChangeListener implements TextWatcher {
        private OnTextChangeListener onTextChangeListener;

        public TextChangeListener(OnTextChangeListener onTextChangeListener) {
            this.onTextChangeListener = onTextChangeListener;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            onTextChangeListener.onTextChange(s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    }

    private static class TextWatcherInfo {
        private EditText editText;
        private int listenLength;

        public EditText getEditText() {
            return editText;
        }

        public void setEditText(EditText editText) {
            this.editText = editText;
        }

        public int getListenLength() {
            return listenLength;
        }

        public void setListenLength(int listenLength) {
            this.listenLength = listenLength;
        }
    }

    public interface OnTextChangeListener {
        void onTextChange(String string);
    }
}
